from django.shortcuts import render
from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import RetrieveCacheResponseMixin

from . import serializers
from .models import Area

# Create your views here.


class AreasViewSet(RetrieveCacheResponseMixin, ReadOnlyModelViewSet):
    ''' """省市区三级联动数据
    list:
    获取省级数据

    retrieve:
    获取城市和区县数据
    """'''
    #指定查询集
    # queryset = Area.objects.all()
    pagination_class = None
    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    #指定序列化器
    def get_serializer_class(self):
        if self.action == 'list':
            return serializers.AreasSerializer
        else:
            return serializers.SubsAreasSerializer